package com.cabrida.livestar.main;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.model.SearchData;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;
/**
 * 搜索adapter
 */
public class SearchAdapter extends BaseAdapter {
	private Context context;
	private List<SearchData> persons=new ArrayList<SearchData>();
	private LayoutInflater inflater;

	public SearchAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}
	
	public SearchAdapter(Context context, List<SearchData> persons) {
		this.context = context;
		this.persons = persons;
		this.inflater = LayoutInflater.from(context);

	}
	
	public void refreshData(List<SearchData> persons){
		this.persons = persons;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return persons.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return persons.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder = null;
		SearchData person = persons.get(position);
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = inflater.inflate(R.layout.activity_search_lv_item, null);
			viewholder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_lv_item_name);
			viewholder.tv_num_attention = (TextView) convertView
			.findViewById(R.id.tv_num_attention);
			viewholder.iv_header = (CircleImageView) convertView
			.findViewById(R.id.iv_header);
			viewholder.tv_attention = (TextView) convertView
			.findViewById(R.id.tv_attention);
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		
		viewholder.tv_name.setText(person.getS_name());
		viewholder.tv_num_attention.setText(person.getS_booked()+"人关注");
		MyUtils.loadImg(person.getU_avatar(), viewholder.iv_header);
		
		if (!TextUtils.isEmpty(person.getS_booked())) {
			int booked_int = Integer.valueOf(person.getS_status());
			if (booked_int==10) {
				viewholder.tv_attention.setText(context.getResources()
							.getString(R.string.attention_over));
				viewholder.tv_attention.setBackgroundResource(R.drawable.bg_corner_green_white_corner_15);
				viewholder.tv_attention.setTextColor(context.getResources().getColor(R.color.green));
			}else{
				viewholder.tv_attention.setText(context.getResources()
						.getString(R.string.attention));
				viewholder.tv_attention.setBackgroundResource(R.drawable.bg_corner_green_corner_15);
				viewholder.tv_attention.setTextColor(context.getResources().getColor(R.color.white));
			}
				
		}
		
		viewholder.tv_attention.setTag(position);
		viewholder.tv_attention.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int pos = (Integer) v.getTag();
				String s_udid = persons.get(pos).getSuid();
				Intent intent = new Intent();
				intent.putExtra("s_udid", s_udid);
				intent.setAction(MainActivity.ACTION_VLOG_ATTENTION_LIST);
				context.sendBroadcast(intent);
			}
		});
		
		return convertView;
	}

	class ViewHolder {
		TextView tv_name;
		TextView tv_num_attention;
		CircleImageView iv_header;
		TextView tv_attention;
	}

}
